<?php

namespace app\mobile\controller;

use think\Controller;
use think\Db;
use Myredis\Myredis;
use think\Session;
use think\Request;
class Index extends Controller
{
   public function index()
   {
       $cate=Db::table('xo_admin_category')->field(['id,name,pid'])->where('path','like','0,%')->select();

       $num=array();
       $n=count($cate);

       if(!empty($cate)){
           for ($i=0; $i<8;$i++)
           {
               $a=mt_rand(0,$n-1);
               if(empty(in_array($a,$num))){
                   $num[]=$cate[$a];
               }
           }
       }





       if (Request::instance()->session('tel')) {
           $tel = Request::instance()->session('tel');
           $res = Db::table('xo_user')->field('pwd', true)->where('tel', $tel)->find();
           Session::set('name', $res['nickname']);
           Session::set('uid', $res['id']);
           Session::set('icon', $res['icon']);
       }
       // $stoken = Request::instance()->session('token');
       // $rtoken = MyRedis::get(Request::instance()->session('tel') . 'token');
       // if (!empty($rtoken)) {
       //     if ($stoken != $rtoken) {
       //         Session::clear();
       //         return $this->success('账号异常登录,请注意账号安全', url('index/index/index'));
       //     }
       // }
       $res=Db::table('xo_admin_banner')->field(true)->where(['status'=>'1'])->select();
       $data=Db::table('xo_goods_up')->field(true)->where(['check'=>'1'])->limit('30')->select();
       return view('index',['res'=>$res,'data'=>$data,'num'=>$num]);
   }


   public function goods()
   {
    $id=input('id');
    $str=$id.',';
    $res=Db::table('xo_admin_category')->field(true)->where('path','like','%'.$str.'%')->select();
        foreach($res as $k =>$v){
            $str.=$v['id'].',';
        }
       $str=rtrim($str,',');
       $map['cid']=array('in',$str);
       $data=Db::table('xo_goods_up')->field(true)->where($map)->select();
    return view('index/goods',['data'=>$data]);
   }


   public function search()
   {
       $str=input('str');
//       $data = Db::table('xo_goods_up')->field(true)->where(['pname' => ['like', $str], 'check' => '1'])->select();

       $data = Db::table('xo_goods_up')->field(true)->where('pname','like','%'.$str.'%')->select();
        if(empty($data)){
            return $this->error('没有搜到该宝贝');
        }
       return view('index/goods', ['data' => $data]);
   }
}
